package com.gsn.poker.play;

import java.util.ArrayList;
import java.util.List;

public class ChipGroupLogic{

	public static final int CHIP_1 = 0;
	public static final int CHIP_5 = 1;
	public static final int CHIP_10 = 2;
	public static final int CHIP_50 = 3;
	public static final int CHIP_100 = 4;
	public static final int CHIP_500 = 5;
	public static final int CHIP_1K = 6;
	public static final int CHIP_5K = 7;
	public static final int CHIP_10K = 8;
	public static final int CHIP_50K = 9;
	public static final int CHIP_100K = 10;
	public static final int CHIP_1M = 11;
	public static final int CHIP_5M = 12;
	public static final int CHIP_10M = 13;
	public static final int CHIP_50M = 14;
	public static final int CHIP_100M = 15;
	public static final int CHIP_1B = 16;
	public static final int CHIP_10B = 17;
	public static final int CHIP_50B = 18;
	public static final int CHIP_100B = 19;
	public static final int CHIP_LENGTH = 20;

	public static final int SPEED = 20;
	public List<Long> chipList = new ArrayList<Long>();// mảng chứa số lượng các
														// chip
	public List<Long> chipValueList = new ArrayList<Long>();// mảng chưa giá trị
															// của các loại chip
	public List<Long> finalChipList = new ArrayList<Long>();
	public List<Long> finalChipValueList = new ArrayList<Long>();
	/**
	 * tạo nhóm các chip dựa vào tiền
	 * 
	 * @param money
	 */
	public ChipGroupLogic() {
		
	}
	public static int getType(long value){
		int type = 0;
		if (value == 1){
			type = ChipGroupLogic.CHIP_1;
		} else if (value == 5){
			type = ChipGroupLogic.CHIP_5;
		} else if (value == 10){
			type = ChipGroupLogic.CHIP_10;
		} else if (value == 50){
			type = ChipGroupLogic.CHIP_50;
		} else if (value == 100){
			type = ChipGroupLogic.CHIP_100;
		} else if (value == 500){
			type = ChipGroupLogic.CHIP_500;
		} else if (value == 1000){
			type = ChipGroupLogic.CHIP_1K;
		} else if (value == 5000){
			type = ChipGroupLogic.CHIP_5K;
		} else if (value == 10000){
			type = ChipGroupLogic.CHIP_10K;
		} else if (value == 50000){
			type = ChipGroupLogic.CHIP_50K;
		} else if (value == 100000){
			type = ChipGroupLogic.CHIP_100K;
		} else if (value == 1000000){
			type = ChipGroupLogic.CHIP_1M;
		} else if (value == 5000000){
			type = ChipGroupLogic.CHIP_5M;
		} else if (value == 10000000){
			type = ChipGroupLogic.CHIP_10M;
		} else if (value == 50000000){
			type = ChipGroupLogic.CHIP_50M;
		} else if (value == 100000000){
			type = ChipGroupLogic.CHIP_100M;
		} else if (value == 1000000000){
			type = ChipGroupLogic.CHIP_1B;
		} else if (value == 10000000000l){
			type = ChipGroupLogic.CHIP_10B;
		} else if (value == 50000000000l){
			type = ChipGroupLogic.CHIP_50B;
		} else if (value == 100000000000l){
			type = ChipGroupLogic.CHIP_100B;
		}
		return type;
	}
	
	/**
	 * reset giá trị các mảng chứa chip
	 */
	public void resetChipList() {
		chipList.clear();
		chipValueList.clear();
		for (int i = 0; i < CHIP_LENGTH; i++) {
			chipList.add(0l);
			switch (i) {
			case CHIP_1:
				chipValueList.add(1l);
				break;
			case CHIP_5:
				chipValueList.add(5l);
				break;
			case CHIP_10:
				chipValueList.add(10l);
				break;
			case CHIP_50:
				chipValueList.add(50l);
				break;
			case CHIP_100:
				chipValueList.add(100l);
				break;
			case CHIP_500:
				chipValueList.add(500l);
				break;
			case CHIP_1K:
				chipValueList.add(1000l);
				break;
			case CHIP_5K:
				chipValueList.add(5000l);
				break;
			case CHIP_10K:
				chipValueList.add(10000l);
				break;
			case CHIP_50K:
				chipValueList.add(50000l);
				break;
			case CHIP_100K:
				chipValueList.add(100000l);
				break;
			case CHIP_1M:
				chipValueList.add(1000000l);
				break;
			case CHIP_5M:
				chipValueList.add(5000000l);
				break;
			case CHIP_10M:
				chipValueList.add(10000000l);
				break;
			case CHIP_50M:
				chipValueList.add(50000000l);
				break;
			case CHIP_100M:
				chipValueList.add(100000000l);
				break;
			case CHIP_1B:
				chipValueList.add(1000000000l);
				break;
			case CHIP_10B:
				chipValueList.add(10000000000l);
				break;
			case CHIP_50B:
				chipValueList.add(50000000000l);
				break;
			case CHIP_100B:
				chipValueList.add(100000000000l);
				break;
			}
		}
	}	
	
	private void reduceChip() {	
		finalChipList.clear();
		finalChipValueList.clear();
		int max = 5;
		int count = 0;
		for (int i = chipList.size() - 1; i >= 0; i--){
			if (chipList.get(i) > 0){
				count++;				
				finalChipList.add(chipList.get(i));
				finalChipValueList.add(chipValueList.get(i));
				if (count >= max)
					break;
			}
		}
	}

	/**
	 * đổi tiền sang số chip
	 * 
	 * @param money
	 *            return ntype số loại chip
	 */
	public int convertMoneyToChip(long money) {
		if (money < 0)
			return 0;
		resetChipList();
		int nTypeChip = 0; // tổng số loại chip sử dụng
		long tmp;
		long value;
		tmp = money;
		for (int i = CHIP_LENGTH - 1; i > -1; i--) {
			value = tmp / chipValueList.get(i);
			tmp = tmp % chipValueList.get(i);
			// tmp -= (value * chipValueList[i]);
			chipList.set(i, value);
			if (chipList.get(i) > 0) {
				nTypeChip++;
			}
		}
		reduceChip();
		return nTypeChip;
	}
	
	public static void main(String[] args) {
		ChipGroupLogic chip = new ChipGroupLogic();
		int money = 124232;
		chip.convertMoneyToChip(money);
		System.out.println("total : " + money);
		for (int i = 0; i < chip.chipList.size(); i++){			
			System.out.println(chip.chipValueList.get(i) + "$ x " + chip.chipList.get(i));
		}
		System.out.println("-------------");
		System.out.println("total : " + money);
		for (int i = 0; i < chip.finalChipList.size(); i++){			
			System.out.println(chip.finalChipValueList.get(i) + "$ x " + chip.finalChipList.get(i));
		}
	}
}
